<?php

/**
 * adventure actions.
 *
 * @package    s-test
 * @subpackage adventure
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class adventureActions extends sfActions
{
  public function executeTopWatchedAdventure(sfWebRequest $request){
      $this->ktd_adventures = KTDAdventure::getTopWatchedAdventure(5);
  }
  public function executeWatch(sfWebRequest $request){
      $this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id')));
      $this->forward404Unless($this->ktd_adventure);
      if(!$this->getUser()->getGuardUser()->isWatching($this->ktd_adventure)){
          $this->getUser()->getGuardUser()->watch($this->ktd_adventure);
      }
      else
          $this->getUser()->getGuardUser()->unwatch($this->ktd_adventure);
      $this->redirect($this->generateUrl("show_adventure", $this->ktd_adventure));
      
  }
  public function executeFree_adventure_index(sfWebRequest $request){
      $this->advs = KTDAdventure::getFreeAdventure();
  }
  public function executeShow(sfWebRequest $request)
  {
    $this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id')));
    $this->pager = $this->ktd_adventure->getPagerActions(sfConfig::get("app_max_actions_on_adventure"), $request->getParameter('page', 1));
    $this->ktd_actions = $this->pager->getResults();
    
    $this->ktd_parser = KTDCodeParser::getSingleton();
    if($this->ktd_adventure->isNew())
            $this->ktd_adventure->setOld();
    $this->forward404Unless($this->ktd_adventure);

    $this->user_can_create_sum = $this->getUser()->isAuthenticated() && $this->getUser()->getGuardUser()->hasActualCharacter() && ($this->ktd_adventure->isAnActor($this->getUser()->getGuardUser()->getActualCharacter()) && $this->getUser()->hasPermission("edit_adventure_sum")) || $this->getUser()->hasPermission("admin");
  }
  public function executeEditSum(sfWebRequest $request){
    $this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasActualCharacter(), "Vous n'avez pas de personnage selectionné !");
    $this->forward404Unless(($this->ktd_adventure->isAnActor($this->getUser()->getGuardUser()->getActualCharacter()) && $this->getUser()->hasPermission("edit_adventure_sum")) || $this->getUser()->hasPermission("admin"),'Votre personnage actuellement utilisé ne fait pas partis de cette aventure, vous ne pouvez pas éditer le résumé !');
    $this->form = new KTDAdventureSumForm($this->ktd_adventure);
  }
  public function executeShowSum(sfWebRequest $request){
    
    $this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id')));
  
    $this->user_can_create_sum = $this->getUser()->isAuthenticated() && $this->getUser()->getGuardUser()->hasActualCharacter() && ($this->ktd_adventure->isAnActor($this->getUser()->getGuardUser()->getActualCharacter()) && $this->getUser()->hasPermission("edit_adventure_sum")) || $this->getUser()->hasPermission("admin");
  }
  public function executeUpdateSum(sfWebRequest $request){
    $this->ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasActualCharacter(), "Vous n'avez pas de personnage selectionné !");
    $this->forward404Unless(($this->ktd_adventure->isAnActor($this->getUser()->getGuardUser()->getActualCharacter()) && $this->getUser()->hasPermission("edit_adventure_sum")) || $this->getUser()->hasPermission("admin"),'Votre personnage actuellement utilisé ne fait pas partis de cette aventure, vous ne pouvez pas éditer le résumé !');
    
    $this->form = new KTDAdventureSumForm($this->ktd_adventure);
    $this->processForm($request, $this->form);
    $this->setTemplate("editSum");
    
  }
  public function executeNew(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('create_adventure'), "Vous n'avez pas la permission de créer une nouvelle aventure");
    $this->forward404Unless($this->getUser()->getGuardUser()->hasActualCharacter(), "Vous n'avez pas de personnage selectionné !");
    $this->forward404Unless($this->ktd_region = Doctrine::getTable('KTDRegion')->find(array('id' => $request->getParameter('id'))), sprintf("La région où vous souhaitez jouer n'existe pas ou plus."));

    $ktd_adventure = new KTDAdventure();
    $ktd_adventure->setKTDRegion($this->ktd_region);

    $this->form = new KTDAdventureForm($ktd_adventure);
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('create_adventure'), "Vous n'avez pas la permission de créer une nouvelle aventure");
    $this->forward404Unless($this->ktd_region = Doctrine::getTable('KTDRegion')->find(array('id' => $request->getParameter('region_id'))), sprintf("La région où vous souhaitez jouer n'existe pas ou plus."));

    //On pré-crée l'aventure en y ajoutant la région où on la crée.
    $ktd_adventure = new KTDAdventure();
    $ktd_adventure->setKTDRegion($this->ktd_region);

    $this->form = new KTDAdventureForm($ktd_adventure);
    //On déclare quel personnage a crée cette aventure
    $this->form->setOption("character_id", $this->getUser()->getGuardUser()->getActualCharacterId());

    //Traitement du formulaire...
    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }
  public function executeDelete(sfWebRequest $request)
  {
    $this->forward404Unless($this->getUser()->getGuardUser()->hasPermission('delete_adventure'), "Vous n'avez pas la permission de supprimer cette aventure");

    $this->forward404Unless($ktd_adventure = Doctrine::getTable('KTDAdventure')->find(array($request->getParameter('id'))), sprintf('Cette aventure n\'existe pas ou plus.'));

    $ktd_region = $ktd_adventure->getKTDRegion();
    $ktd_adventure->delete();

    $this->redirect($this->generateUrl("show_region", $ktd_region));
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
         //On envoit l'évenement
         KTDEventManager::getSingleton()->handleAdventure(KTDAdventureHandler::$onCreate,
                                                  array("player"    => $this->getUser()->getGuardUser(),
                                                        "amount"    => sfConfig::get("app_amount_on_adventure_post")));

         $ktd_adventure = $form->save();
         $this->redirect($this->generateUrl("show_adventure", $ktd_adventure));
    }
  }
}
